layui.config({
    base: '../authtree/extends/',
}).extend({
    authtree: 'authtree',
});

var userId;
var $
layui.use(['jquery', 'authtree', 'form', 'layer'], function () {
    userId = top.userId;
    $ = layui.jquery;
    layer = layui.layer;

    //加载用户的ip白名单列表
    /*
    $.ajax({
        url: '/admin/getIPList'
        , dataType: 'json'
        , data: {
            "userId": userId
        }
        , success: function (res) {
            if (res.code == 0) {
                var ips = res.data.ips;
                $('#ips').val(res.trim.length == 0 ? null : ips);
            }
        }
    });
    */
});


function refreshAccessSecret() {
    var index = parent.layer.load(1, {
        shade: [0.3, '#000']
    });
    $.ajax({
        url: '/admin/refresh',
        dataType: 'json',
        data: {
            "accessSecret": "1",
            "accessKey": $('#accessKey').val(),
            "userName": $('#userName').val(),
            "id": $('#userId').val()
        },
        success: function (res) {
            if (res.code == 0) {
                $('#accessSecret').val(res.data.accessSecret);
            } else {
                parent.layer.msg(res.msg, {icon: 2});
            }
        }
        , complete: function (res) {
            parent.layer.close(index);
        }
    });
}

function refreshSignToken() {
    var index = parent.layer.load(1, {
        shade: [0.3, '#000']
    });
    $.ajax({
        url: '/admin/refresh',
        dataType: 'json',
        data: {
            "signToken": "1",
            "accessKey": $('#accessKey').val(),
            "userName": $('#userName').val(),
            "id": $('#userId').val()
        },
        success: function (res) {
            if (res.code == 0) {
                $('#signToken').val(res.data.signToken);
            } else {
                parent.layer.msg(res.msg, {icon: 2});
            }
        }
        , complete: function (res) {
            parent.layer.close(index);
        }
    });
}

function updateIps() {
    var index = parent.layer.load(1, {
        shade: [0.3, '#000']
    });
    var ips = $('#ips').val();
    if (ips.trim().length == 0) {
        ips = null;
    }
    $.ajax({
        url: '/admin/refresh'
        , dataType: 'json'
        , data: {
            "id": $('#userId').val(),
            "userName":$('#userName').val(),
            "ipWhilteList": ips
        }
        , success: function (res) {
            if (res.code == 0) {
                $('#ips').val(res.data.ipWhilteList);
                parent.layer.msg('修改成功', {icon: 1});
            } else {
                parent.layer.msg(res.msg, {icon: 2});
            }
        }, complete: function (res) {
            parent.layer.close(index);
        }
    });
}

Array.prototype.indexOf = function (val) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == val) return i;
    }
    return -1;
};

Array.prototype.remove = function (val) {
    var index = this.indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
    return this;
};